package cn.com.xinjinfu.pdoulcProduct.activity.base;


import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.umeng.common.message.UmengMessageDeviceConfig;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;

import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;


/**
 * Created by android-adt on 2015/12/29.
 */
public class BaseActivity extends FragmentActivity {

    public static int WIDTH;
    public static int HIGHT;
    public Context mContext;
    public String mPageName ;
    public static int statusBarHeight;
    PushAgent mPushAgent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        mPageName= this.getClass().getName();

        // 去掉标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);

//        ((BaseApplication) getApplication()).activitys.add(this);
//        //判断是否锁屏
//
//        //屏幕适配
//        Display dis = getWindowManager().getDefaultDisplay();
//        WIDTH=dis.getWidth();
//        HIGHT=dis.getHeight();
//        if(BaseApplication.WIDTH==0){
//            BaseApplication.WIDTH=WIDTH;
//            BaseApplication.HEIGHT=HIGHT;
//        }
//        Rect frame = new Rect();
//        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
//        statusBarHeight = frame.top;


        //固定竖屏(禁止横屏)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


        MobclickAgent.setDebugMode(true);
        /**
         * 友盟推送
         */
        mPushAgent = PushAgent.getInstance(mContext);
        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        mPushAgent.onAppStart();
        mPushAgent.enable();
        String device_token = UmengRegistrar.getRegistrationId(mContext);

        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(mContext, MobclickAgent.EScenarioType.E_UM_NORMAL);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(mPageName);		//友盟统计分析对act统计
        MobclickAgent.onResume(mContext);			//友盟统计分析对act统计
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd( mPageName );
        MobclickAgent.onPause(mContext);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}
